<template>
	<view class="wraper">
		<view class="search" v-if="search_btn">
			<view @tap="openSearch">
				<icon type="search" size="13"></icon>
				<text>搜索</text>
			</view>
		</view>

		<view class="search_input" v-if="search_friend">
			<view>
				<icon type="search" size="13"></icon>
				<input
					placeholder="搜索"
					placeholder-style="color:#CFCFCF;line-height:20px;font-size:12px;"
					auto-focus
					confirm-type="search"
					type="text"
					@confirm="onSearch"
					@input="onInput"
					:value="input_code"
				/>
				<icon type="clear" size="13" @tap.stop="clearInput" v-if="show_clear"></icon>
			</view>
			<text @tap="cancel">取消</text>
		</view>

		<scroll-view class="content" enable-back-to-top scroll-y="true" scroll-with-animation="true" :style="'height: ' + second_height + 'px'">
			<checkbox-group v-for="(item, index) in renderList" :key="index" :data-item="item" :id="itemid" :data-id="group.id" @change="checkboxChange">
				<view class="tap_mask" :data-username="item.member || item.owner">
					<view class="address_bottom" :data-username="item.member || item.owner">
						<image src="/static/images/theme@2x.png" :data-username="item.member || item.owner"></image>
						<text :data-username="item.member || item.owner">{{ item.member || item.owner }}</text>
						<checkbox :value="item.member || item.owner" :checked="item.checked" :disabled="item.disabled"></checkbox>
					</view>
				</view>
			</checkbox-group>
		</scroll-view>

		<view class="btnWraper">
			<button class="button" @tap="startConfr">{{ buttonText }}</button>
			<button class="button red" @tap="goBack">返回</button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			search_btn: true,
			search_friend: false,
			groupMember: [],
			serchList: [],
			checkedValue: [],
			renderList: [],
			buttonText: '发起会议',
			second_height: 450,
			show_clear: false,
			show_mask: false,
			gotop: false,
			input_code: ''
		};
	},

	components: {},
	props: {
		//接收父组件传过来的值
		username: null,
		action: null
	},
	beforeMount: function(options) {
		console.log('邀请页面的参数', this);
		console.log(this.username);
		console.log(this.action);
		var that = this; // 获取系统信息

		uni.getSystemInfo({
			success: function(res) {
				console.log('height=' + res.windowHeight);
				console.log('width=' + res.windowWidth); // 计算主体部分高度,单位为px

				// second部分高度 = 利用窗口可使用高度 - first部分高度（这里的高度单位为px，所有利用比例将300rpx转换为px）
				that.second_height = res.windowHeight - (res.windowWidth / 750) * 300
			}
		});

		if (this.action == 'invite') {
			this.buttonText = '邀请'
		}

		var roomId = this.username && this.username.groupId;
		roomId && this.getGroupMember(roomId);
	},
	methods: {
		getGroupMember: function(roomId) {
			var me = this; // 获取群成员

			var pageNum = 1,
				pageSize = 1000;
			var options = {
				pageNum: pageNum,
				pageSize: pageSize,
				groupId: roomId,
				success: function(resp) {
					console.log('获取群成员', resp);

					if (resp && resp.data) {
						me.groupMember = resp.data
						me.getRenderList(resp.data);
					}
				},
				error: function(err) {}
			};
			console.log(this.$im.conn)
			this.$im.conn.listGroupMember(options);
		},

		getRenderList(list) {
			console.log('list', list);
			let serchList = list.data.map(item => {
				for (var i = 0; i < this.checkedValue.length; i++) {
					if ((item.member && item.member.indexOf(this.checkedValue[i]) != -1) || (item.owner && item.owner.indexOf(this.checkedValue[i]) != -1)) {
						item.checked = true;
						return item;
						break;
					} else {
						item.checked = false;
					}
				}

				return item;
			});
			const userId = this.$im.conn.context.userId
			serchList.forEach((item, index) => {
				if (item.member == userId || item.owner == userId) {
					item.disabled = true;
					serchList.splice(index, 1)
				}
			});
			this.renderList = serchList
			console.log('serchList >>>>', serchList);
		},

		checkboxChange: function(e) {
			console.log('checkbox发生change事件，携带value值为：', e);

			if (this.checkedValue.indexOf(e.detail.value) == -1 && e.detail.value[0]) {
				this.checkedValue.push(e.detail.value[0]);
				console.log(this.checkedValue);
			} else {
				let value = e.target.dataset.item.name;
				this.checkedValue.splice(this.checkedValue.indexOf(value), 1);
				console.log(this.checkedValue);
			}
		},
		openSearch: function() {
			this.search_btn = false;
			this.search_friend = true;
			this.show_mask = true;
			this.gotop = true;
		},
		cancel: function() {
			this.search_btn = true;
			this.search_friend = false;
			this.gotop = false;
		},

		onInput(e) {
			let inputValue = e.detail.value;

			if (inputValue) {
				tthis.show_clear = true
			} else {
				this.show_clear = false
			}
		},

		clearInput: function() {
			this.input_code = '';
			this.show_clear = false
		},
		cancel: function() {
			this.search_btn = true;
			this.search_friend = false;
			let original = this.groupMember || [];
			this.getRenderList(original);
		},
		onSearch: function(val) {
			let searchValue = val.detail.value;
			let member = this.groupMember;
			let serchList = [];
			member.forEach((item, index) => {
				if (String(item.member).indexOf(searchValue) != -1 || String(item.owner).indexOf(searchValue) != -1) {
					serchList.push(item);
				}
			});

			this.getRenderList(serchList);
		},

		startConfr() {
			this.$emit('startConfr', {
				detail: {
					confrMember: this.checkedValue,
					action: this.action,
					groupId: this.username.groupId
				}
			});
			this.checkedValue = []
		},

		goBack() {
			this.checkedValue = []
			this.$emit('goBack');
		}
	}
};
</script>
<style>
page{
	height: 100%;
	width: 100%;
}
.wraper{ 
	height: 100%;
	position: fixed;
	z-index: 999;
	width: 100%;
	top: 0;
	left: 0;
	background: #fff;
}
.main_title{
	background-color: #fff;
	width: 100%;
	height: 128rpx;
	position: fixed;
	top: 0;
	z-index: 9;
}
.main_title text{
	line-height: 96rpx;
	font-size: 64rpx;
	font-weight: 400;
	margin-left: 32rpx;
}
.main_title_hide{
	top: -128rpx;
	transition: top 0.5s;
}
.main_title_show{
	top: 0;
	transition: top 0.5s;
}
.search,.search_input {
	width: 100%;
	height: 88rpx;
	background-color: #fff;
	display: flex;
	align-items: center;
	top: 0;
	left: 0;
}
.search {
	justify-content: space-around;
}
.search_input {
	justify-content: space-around;
}
.search view, .search_input view {
	height: 64rpx;
	line-height: 64rpx;
	background-color: #F5F5F5;
	border-radius: 16rpx;
	text-align: center;
	display: flex;
	align-items: center;
	margin: 0 32rpx;
}
.search view {
	width: 100%;
	justify-content: center;
	align-items: center;
}
.search image {
	display: block;
	width: 6%;
	height: 53%;
	margin-right: 10rpx;
}
.search_input view {
	padding-left: 24rpx;
	text-align: left;
	flex: 1;
	margin: 0 28rpx 0 32rpx;
}
.search icon,.search_input icon {
	display: inline-block;
	margin: 8rpx 12rpx 0;
	font-size: 24rpx;
}
.search text {
	font-size: 30rpx;
	color: #9B9B9B;
}

.search_input text {
	display: inline-block;
	height: 60rpx;
	line-height: 60rpx;
	font-size: 30rpx;
	color: #0873DE;
	margin-right: 32rpx;
}
.search_input input {
	font-size: 28rpx;
	width: 90%;
}

.scrollWraper{
	flex: 1;
	position: relative;
}

/*.contain {
	margin-top: 40px;
}*/
.chat,.chat_groups,.chat_lists{
	position: relative;
	width: 100%;
	height: 90rpx;
	display: flex;
	align-items: center;
}
.chat,.chat_groups {
	border-bottom: 0.5px solid #E5E5E5;
}
.chat image {
	width: 6%;
	height: 50%;
	margin:0 26rpx 0 20rpx;
}
.chat .em-msgNum {
	position: absolute;
	right: 20rpx;
	height: 34rpx;
	border-radius: 20rpx;
	min-width: 30rpx;
	background: #f04134;
	color: #fff;
	line-height: 40rpx;
	text-align: center;
	padding: 0px 4rpx 4rpx 4rpx;
}
.chat_groups image {
	width: 8%;
	height: 54%;
	margin:0 20rpx 0 16rpx;
}
.chat_lists image {
	width: 12%;
	height: 100%;
	margin-right: 4rpx;
}
.chat text,.chat_groups text,.chat_lists text {
	font-size: 28rpx;
}
.numbers {
	width: 100%;
	height: 100rpx;
}
.nbr_header {
	height: 2rpx;
	background-color: #D4D4D4;
	line-height: 40rpx;
	padding-left: 32rpx;
}
.nbr_body {
	width: 70%;
	height: 100rpx;
	display: inline-block;
	font-size: 32rpx;
	line-height: 100rpx;
	margin-right: 0;
	float: left;
}
.mask {
	background-color: black;
	opacity: 0.4;
	position: absolute;
	top: 40rpx;
	left: 0;
	right: 0;
	bottom: 0;
}
.delete {
	width: 60rpx;
	height: 70rpx;
	float: right;
	margin-top: 12rpx;
}
.delete image {
	width: 32rpx;
	height: 4rpx;
}
.info {
	margin-top: 10rpx;
	width: 12%;
	height: 80rpx;
	display: inline-block;
	float: left;
}
.info image {
	width: 68%;
	height: 70%;
	display: inline-block;
	margin: 12rpx auto auto 12rpx;
}

.otherItem{
	height: 108rpx;
	display: flex;
  	align-items: center;
  	border-bottom: 0.5px solid #E5E5E5;
  	margin:0 32rpx;
  	position: relative;
}
.otherItem image{
	width: 72rpx;
	height: 72rpx;
	margin: 0 22rpx 0 0;
}
.otherItem text{
	font-size: 34rpx;
	color: #000;
}
.otherItem .line{
	position: absolute;
	bottom: 0;
	width: 100%;
	height:.5px;
}

.content{
	box-sizing: border-box; 
} 

.location{
	width: 100%;
} 
.location_top{
	height: 76rpx;
	line-height: 76rpx;
	background: #f4f4f4;
	color: #606660;
	font-size: 28rpx;
	padding: 0 20rpx;
} 
.location_bottom{
	height: 140rpx;
	line-height: 140rpx;
	color: #d91f16;
	font-size: 28rpx;
	border-top: 0.5px #E5E5E5 solid;
	border-bottom: 0.5px #E5E5E5 solid;
	padding: 0 20rpx;
	align-items: center;
	display: -webkit-flex;
} 
.address_top{
	height:40rpx;
	line-height:40rpx;
	background:#FAFAFA;
	color:#9B9B9B;
	font-size:20rpx;
	padding:0 32rpx;
} 
.address_bottom{
	height: 108rpx;
	line-height: 108rpx;
	background: #fff;
	color: #000000;
	font-size: 34rpx;
	border-bottom: 0.5px #E5E5E5 solid;
	margin: 0 32rpx;
	display: flex;
	align-items: center;
} 
.address_bottom image{
	width: 72rpx;
	height: 72rpx;
	margin-right: 24rpx;
}
.address_bottom text{
	overflow: hidden;
	text-overflow: ellipsis;
	flex: 1;
}
.location_img{
	width: 48rpx;
	height: 48rpx;
	position: absolute;
	right: 20rpx;
	top: 125rpx;
} 
.add_city{
	width: 228rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	border: 0.5px solid #E5E5E5;
	color: #000000;
	margin-right: 20rpx;
} 
.add_citying{
	width: 228rpx;
	height: 60rpx;
	line-height: 60rpx;
	text-align: center;
	border: 2rpx solid #09bb07;
	color: #09bb07;
	margin-right: 20rpx;
} 
.orientation{
	white-space:normal;
	display: inline-block;
 	width: 45rpx;
 	height:50rpx;
	font-size: 28rpx;
	font-weight: bold;
 	color: rgb(88, 87, 87);
 	text-align: center;
} 
.orientation_region{
	padding: 10rpx 0px; 
	width: 28rpx;
	font-size: 20rpx;
	position: fixed;
	top: 50%;
	right: 0;
	transform:translate(0,-50%);
	border-radius: 10px
} 
.orientation_city{
	height: 24rpx;
	width: 24rpx;
	line-height: 24rpx;
	font-size: 18rpx;
	color: #000;
	text-align: center;
} 
.active{
	color: #fff;
	background-color: #0873DE;
	border-radius: 50%;
}
.list-fixed{
	position: fixed;
	width: 100%;
	z-index: 999; 
	height: 56rpx;
	line-height: 56rpx; 
	background: #EBEBEB;
	color: #999999;
	font-size: 28rpx;
	padding: 0 20rpx;
	top: 128rpx;
}
.fixed-title{
	color:#2cc1d1;
}

.goTop{
	top: 0;
	transition: top 0.5s;
	padding-bottom: 104rpx!important;
}
.goTopX{
	top: 0rpx;
	transition: top 0.5s;
	padding-bottom: 140rpx!important;
}
.goback{
	top: 128rpx;
	transition: top 0.5s;
}
.fixedTitleTop{
	top: 0;
}
.tap_mask{
	width: 100%;
	overflow: hidden;
}
.btnWraper{
	align-items: center;
	justify-content: center;
	display: flex;
	flex-direction: column;
}
.button{
	width: 80%;
	height: 80rpx;
	background: green;
	border-radius: 40rpx;
	right: 0;
	left: 0;
	color: #fff;
	font-size: 16px;
	margin: 10rpx;
}
.red{
	background: red;
}

</style>
